package com.example.springboothomework.mapper;

import com.example.springboothomework.entity.Employee;
import com.example.springboothomework.entity.dto.EmployeeDTO;
import com.example.springboothomework.entity.excel.EmployeeExcelExport;
import com.example.springboothomework.entity.excel.EmployeeExcelImport;
import com.example.springboothomework.entity.excel.EmployeeExcelImportWithError;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;

/**
 * @author chenjianhao
 * @date 2025/6/7
 *
 * 员工MapStruct映射
 */
@Mapper(componentModel = "spring")
public interface EmployeeMapper {

    /**
     * DTO转实体(忽略id，新增时自增)
     * @param dto
     * @return
     */
    @Mapping(target = "id",ignore = true)
    Employee toEntity(EmployeeDTO dto);

    /**
     * DTO转entity（忽略id，更新时防止ID被覆盖）
     * @param dto
     * @param employee
     */
    @Mapping(target = "id", ignore = true)
    void updateEntity(EmployeeDTO dto,@MappingTarget Employee employee);


    /**
     * 导入类转为实体类
     * @param excelImport 导入类
     * @param companyId 公司ID
     * @return
     */
    @Mapping(target = "id", ignore = true)
    @Mapping(target = "companyId", expression = "java(companyId)")
    Employee importToEntity(EmployeeExcelImport excelImport, Long companyId);


    /**
     * EmployeeExcelImport转为EmployeeExcelImportWithError
     * 用户导入信息+失败原因——>带错误信息的导入类
     * @param excelImport
     * @param errorMsg
     * @return
     */
    @Mapping(target = "errorReason", expression = "java(errorMsg)")
    EmployeeExcelImportWithError importToImportWithError(EmployeeExcelImport excelImport, String errorMsg);

}
